package team.first.movieapiv2.controller;

import cn.hutool.core.bean.BeanUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import dev.morphia.Datastore;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import team.first.movieapiv2.doc.CommentDoc;
import team.first.movieapiv2.domain.Comment;
import team.first.movieapiv2.domain.Movie;
import team.first.movieapiv2.domain.User;
import team.first.movieapiv2.util.MongoDBUtil;
import team.first.movieapiv2.vo.ApiResponse;
import team.first.movieapiv2.vo.ResponseComment;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapiv2.controller
 * @Date: 2021/12/9 13:19
 * @Description:
 */
@RestController
public class CommentController {

    @GetMapping("/api/v2/movie/{id}/comments")
    @ApiOperation("1.6 GET： 获取指定编号的电影评论")
    public ApiResponse<List<ResponseComment>> getMovieComment(@PathVariable("id") Integer movieId) {
        ApiResponse<List<ResponseComment>> apiResponse = new ApiResponse<>();

        DBCollection movieCollection = MongoDBUtil.getCollection("comment");
        // 建立查询条件
        BasicDBObject filter_dbobject = new BasicDBObject();
        filter_dbobject.put("movieId", movieId);
        // 条件查询
        DBCursor cursor1 = movieCollection.find(filter_dbobject).sort(new BasicDBObject("id", 1));

        // 将结果转为集合
        List<DBObject> dbObjectList = cursor1.toArray();

        // 属性考呗
        List<ResponseComment> responseCommentList = BeanUtil.copyToList(dbObjectList, ResponseComment.class);

        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(responseCommentList);
        return apiResponse;
    }

    /**
     *  2.3 POST 用户对指定电影进行评论
     * @param comment 评论信息
     * @return 返回评论后的信息
     */
    @ApiOperation(value = "2.3 POST 对电影进行评论，返回评论后的电影信息")
    @PostMapping("/api/v2/comment")
    public ApiResponse<Integer> comment(Comment comment) {
        ApiResponse<Integer> apiResponse = new ApiResponse<>();
        DBCollection collection = MongoDBUtil.getCollection("user");

        Datastore datastore = MongoDBUtil.getDataStore();
        datastore.getMapper().mapPackage("team.first.movieapiv2.doc");
        CommentDoc commentDoc = new CommentDoc();
        BeanUtil.copyProperties(comment, commentDoc);

        // 查询用户信息
        // 建立查询条件
        BasicDBObject filter_dbobject = new BasicDBObject();
        filter_dbobject.put("id", comment.getUserId());

        // 条件查询
        DBCursor cursor = collection.find(filter_dbobject).sort(new BasicDBObject("id", 1));

        // 将结果转为集合
        List<DBObject> dbObjectList = cursor.toArray();
        // 属性考呗
        List<User> user = BeanUtil.copyToList(dbObjectList, User.class);
        commentDoc.setNickname(user.get(0).getNickname());
        commentDoc.setAvatar(user.get(0).getAvatar());
        datastore.save(commentDoc);

        // 将评论的电影编号作为响应数据返回
        apiResponse.setCode(200).setSuccess(true).setMessage("评论成功").setData(comment.getMovieId());
        return apiResponse;
    }

}
